package com.hup.sokoban.view.customView.mapListView;

import com.hup.sokoban.model.common.MapItem;
import com.hup.sokoban.view.customView.MapLabel;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.ColorUtil;
import com.hup.utils.swing.customView.ImageButton;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;

/**
 * @author hugan
 * @date 2020/4/29
 */
@Log4j2
public class MapListItemView extends JPanel {

    private JPanel root;
    private JLabel labName;
    private MapLabel labMap;
    private ImageButton imgWinning;
    public ImageButton imgPlaying;

    public MapListItemView() {
        Dimension dimension = new Dimension(-1, (int) (100 * LookThemeUtil.getScreenScale()));
        setPreferredSize(dimension);
        setMinimumSize(dimension);
        $$$setupUI$$$();
    }

    public void setMapItem(JList<MapItem> jList, MapItem mapItem, int index, boolean isSelected, boolean cellHasFocus) {
        if (isSelected) {
            setBackground(ColorUtil.DEF_SELECTED_COLOR);
        } else if (cellHasFocus) {
            setBackground(ColorUtil.DEF_MOUSE_IN_COLOR);
        } else {
            setBackground(null);
        }
        labName.setText((index + 1) + "." + mapItem.getName());
        labMap.setParentList(jList, index);
        labMap.setData(mapItem);
        if (mapItem.isWinning()) {
            imgWinning.setVisible(true);
            imgPlaying.setVisible(false);
        } else if (mapItem.isPlaying()) {
            imgWinning.setVisible(false);
            imgPlaying.setVisible(true);
        } else {
            imgWinning.setVisible(false);
            imgPlaying.setVisible(false);
        }
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        labMap = new MapLabel();
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 0, 3, 0);
        root.add(labMap, gbc);
        labName = new JLabel();
        labName.setText("关卡名");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 3, 0, 0);
        root.add(labName, gbc);
        imgWinning = new ImageButton();
        imgWinning.setIcon(new ImageIcon(getClass().getResource("/img/main/icon_winning.gif")));
        imgWinning.setPadding(0);
        imgWinning.setUnitedIconHeight(1.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 3, 0, 0);
        root.add(imgWinning, gbc);
        imgPlaying = new ImageButton();
        imgPlaying.setIcon(new ImageIcon(getClass().getResource("/img/main/icon_playing.gif")));
        imgPlaying.setPadding(0);
        imgPlaying.setUnitedIconHeight(1.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 3, 0, 0);
        root.add(imgPlaying, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return root; }

}
